<?php

namespace app\api\server\entity;

use app\admin\ServerBase;
use app\api\model\entity\EntityCommentApiModel;
use app\api\model\music\MusicApiModel;
use app\api\model\music\MusicCommentApiModel;
use app\api\model\order\OrderEntityApiModel;
use app\api\model\order\OrderMusicApiModel;
use think\facade\Db;
use think\Request;

class EntityServer extends ServerBase
{
    protected $model;
    protected $request;
    protected $default_field = 'id,title,user_id,url,img,price,original_price,play_num,sale';
    /**
     * @var mixed
     */
    protected $param;
    /**
     * @var mixed
     */
    protected $validate;
    /**
     * @var OrderEntityApiModel
     */
    private $OrderEntityModel;
    /**
     * @var EntityCommentApiModel
     */
    private $entityCommentModel;

    function __construct(Request $request)
    {
        $this->request = $request;
        $this->param = $request->param();
        $this->model = new MusicApiModel();
        $this->entityCommentModel = new EntityCommentApiModel();
        $this->OrderEntityModel = new OrderEntityApiModel();
    }

    function get_comment_list()
    {
        [$page, $limit] = $this->getPage($this->param);
        $goods_id = $this->param['goods_id'] ?? 0;
        $type = $this->param['type'] ?? 0;
        if (empty($goods_id) || empty($type)) {
            $this->getMessageCode('code_common.illegal', 1);
            return false;
        }
        [$list, $count] = $this->entityCommentModel->getApiList($goods_id, $type, $page, $limit);
        $result['data'] = $list;
        $result['count'] = $count;
        $this->getMessageCode('adminMessage.success.select', 2);
        return $result;
    }

    function create_comment()
    {
        $goods_id = $this->param['goods_id'] ?? 0;
        $type = $this->param['type'] ?? 0;
        $order_id = $this->param['order_id'] ?? 0;
        if (empty($goods_id) || empty($type)) {
            $this->getMessageCode('code_common.illegal', 1);
            return false;
        }
        $content = $this->request->post('content');
        if (empty($content)) {
            $this->ERROR = '请输入内容';
            return false;
        }
        if (!empty($order_id)) {
            $order = $this->OrderEntityModel->getFindByUserIdAndId($order_id, $this->request->user_id);
            if (empty($order)) {
                $this->ERROR = '订单不存在';
                return false;
            }
            if ($order['status'] <= 10) {
                $this->ERROR = '订单不能评论';
                return false;
            }
        }
        Db::startTrans();
        try {
            $this->entityCommentModel->create_comment($this->request->user_id, $goods_id, $type, $content);
            if (!empty($order)) {
                $this->OrderEntityModel->where('id', $order_id)->save(['status' => 30]);
            }
            Db::commit();
        } catch (\Exception $exception) {
            Db::rollback();
            $this->ERROR = $exception->getMessage();
            return false;
        }

    }
}